//
//  VCSystemTip.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/27.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCSystemTip: VCBase {
    
    // 公告布局
    private lazy var systemTipView: SysemTipView = {
        let ui = SysemTipView()
        let uiY = realHeight + MARGIN
        ui.frame = CGRect(x: MARGIN, y: uiY, width: SCREEN_WIDTH - MARGIN * 2, height: SCREEN_HEIGHT - uiY)
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_view()
        init_date()
        init_event()
        init_listener()
    }
}

extension VCSystemTip {
    
    private func init_listener() -> Void {
        systemTipView.complete = {item in
            let vc = VCWeb()
            vc.pageapi = "systemtip/\(item)"
            self.pushNextVcHideBottomBar(vc: vc)
        }
    }
    
    private func init_event() -> Void {
//        let tip1 = "{\"content\":\"关于国航航班在宁波栎社国际机场航站楼调整的通知1\", \"createtime\":\"2020-02-23\", \"nid\":\"1\"}"
//        let tip2 = "{\"content\":\"关于国航航班在宁波栎社国际机场航站楼调整的通知2\", \"createtime\":\"2020-02-24\", \"nid\":\"2\"}"
//        let tip3 = "{\"content\":\"关于国航航班在宁波栎社国际机场航站楼调整的通知3\", \"createtime\":\"2020-02-25\", \"nid\":\"3\"}"
//        let tip4 = "{\"content\":\"关于国航航班在宁波栎社国际机场航站楼调整的通知4\", \"createtime\":\"2020-02-26\", \"nid\":\"4\"}"
//
//        let t1 = getOrder(str: tip1)!
//        let t2 = getOrder(str: tip2)!
//        let t3 = getOrder(str: tip3)!
//        let t4 = getOrder(str: tip4)!
//
//        systemTipView.notificationInfos = [t1,t2,t3,t4]
    }
    
    func getOrder(str:String) -> NotificationInfo? {
        let order1Data = str.data(using: .utf8)
        guard let dict = try? (JSONSerialization.jsonObject(with: order1Data!, options: .mutableContainers) as! NSDictionary) else {
            print("order1 convert error")
            return nil
        }
        
        let order = NotificationInfo(dict: dict as! [String : String])
        return order
    }
    
    private func init_date() -> Void {
        self.doPost(api: "search/systemtip", dict: NSDictionary()) { (dict) in
            if self.isServerSuccess(dict: dict) != 1200 {
                self.show(text: "暂无公告")
                return
            }
            // 解析数据
            let arrs = dict.value(forKey: SP_BODYREQUEST) as! NSArray
            var temp = [NotificationInfo]()
            for item in arrs {
                let info = NotificationInfo(dict: item as! [String : Any])
                temp.append(info)
            }
            self.systemTipView.notificationInfos = temp
        }
    }
    
    private func init_view() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(systemTipView)
    }
}
